<?php
	include("gen.php");
	$cmd = get_datan("cmd");

	switch($cmd){
		case 1:
			get_community();
		break;
		case 2:
			community_delete();
		break;
		case 3:
			all_community();
		break;
		case 4:
			community_update();
		break;
		case 5:
			community_add();
		break;
		case 6:
			theSubdistrict();
		break;
		case 7:
			community_detail();
		break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
		}


	function get_community(){
			include_once("community.php");
			$id = get_datan("id");
			$c = new community();
			$row = $c -> get_community_id($id);
			//$row = $c -> fetch();

				
			if(!$row){
				echo "not found";
				exit();
			}
			$row = $c -> fetch();

			echo "{";

				echo jsonn("result",1) .",";
				//echo '"community":{';	
				//echo jsonn("Community_id", $row['community_id']) . ",";
				echo jsons("community_name", $row['community_name']) . ",";
				echo jsonn("population", $row['population']) . ",";
				echo jsonn("household", $row['household']);
				//echo "}";
			echo "}";
		}

	function community_delete(){
		include_once("community.php");
		$id = get_datan("id");
		$c = new community();
			$row = $c -> delete_community($id);
			//$row = $c -> fetch();

			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
				echo jsonn("result",1) .",";
				echo '"community":{';	
				echo jsons("community_id", $row['community_id']);
				echo "}";
			echo "}";
		}



	function all_community(){
		include_once("community.php");
			$id = get_datan("id");
			$c = new community();
			$row = $c -> get_all_community();
			
			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
			echo jsonn("result",1) .",";
			echo '"community":[';
			$row = $c -> fetch();
			while($row){
				

			echo "{";
				echo jsonn("community_id", $row['community_id']) . ",";
				echo jsons("community_name", $row['community_name']) . ",";
				echo jsonn("population", $row['population']) . ",";
				echo jsonn("household", $row['household']);
			if($row = $c -> fetch()){
				echo "},";
			}else{
				echo "}";
			}
			}
	
		echo "]";
		echo "}";
	}


	function community_update(){
		include_once("community.php");
			$id = get_datan("id");
			$community_name = get_data("community_name");
			$population = get_datan("population");
			$household = get_datan("household");
			$c = new community();
			//$row = $c -> update_community($id,$community_name,$population, $household);


			if(!$c -> update_community($id,$community_name,$population, $household)){
				echo"{";
					echo jsonn("result", 0) . ",";
					echo jsons("message","error updating user");
				echo"}";
				return;
			}
			echo "{";
				echo jsonn("result",0) . ",";
				echo jsons("message","record updated");
			echo "}";
	}

	function community_add(){
		include_once("community.php");
		$id = get_datan("id");
		$community_name = get_data("community_name");
		$longitude = get_datan("logitude");
		$latitude = get_datan("latitude");
		$subdistrict_id = get_datan("subdistrict_id");
		$population = get_datan("population");
		$household = get_datan("household");
		$c = new community();

		//$row = $c-> add_community($id, $latitude, $longitude, $population, $household, $community_name, $subdistrict_id);

		if(!$c -> add_community($latitude, $longitude, $population, $household, $community_name, $subdistrict_id)){
				echo"{";
					echo jsonn("result", 0) . ",";
					echo jsons("message","error updating user");
				echo"}";
				return;
			}
			echo "{";
				echo jsonn("result",0) . ",";
				echo jsons("message","record added");
			echo "}";

	}

	function theSubdistrict(){
		include_once("community.php");
		$id = get_datan("id");
		$community_name = get_data("community_name");
		$subdistrict_id = get_datan("subdistrict_id");

		$c =  new community();
		$row = $c -> subdistrict($subdistrict_id);

		$row = $c -> fetch();

			echo "{";

				echo jsonn("result",1) .",";
				//echo '"community":{';	
				//echo jsonn("Community_id", $row['community_id']) . ",";
				echo jsons("community_name", $row['community_name']) . ",";
				echo jsonn("subdistrict_id", $row['subdistrict_id']);
				//echo "}";
			echo "}";

	}

	function community_detail(){
	

			include_once("community.php");
			$id = get_datan("id");
			$c = new community();
			$row = $c -> get_community_id($id);
			//$row = $c -> fetch();

				
			if(!$row){
				echo "not found";
				exit();
			}
			$row = $c -> fetch();

			echo "{";

				echo jsonn("result",1) .",";
				//echo '"community":{';	
				//echo jsonn("Community_id", $row['community_id']) . ",";
				echo jsonn("community_id", $row['community_id']) . ",";
				echo jsons("community_name", $row['community_name']) . ",";
				echo jsonn("longitude", $row['longitude']) . ",";
				echo jsons("latitude", $row['latitude']) . ","; 
				echo jsonn("population", $row['population']) . ",";
				echo jsons("household", $row['household']) . ",";
				echo jsonn("subdistrict_id", $row['subdistrict_id']);
				echo "}";
			
	}

?>
